import android.content.Context
import com.ck.express.vo.LoginVO

// 用户会话管理 - UserSession.kt
class UserSession(context: Context) {
    private val prefs = context.getSharedPreferences("LogisticsPrefs", Context.MODE_PRIVATE)

    fun saveUserInfo(data: LoginVO) {
        prefs.edit().apply {
            putString("txId", data.txId)
            putString("serverTime", data.serverTime)
            putString("userCode", data.userCode)
            putString("userName", data.userName)
            putString("userId", data.userId)
            putString("orgCode", data.orgCode)
            putString("orgName", data.orgName)
            putString("orgType", data.orgType)
            putString("token", data.token)
            putString("signature", data.signature)
            apply()
        }
    }

    fun saveGridSchemeId(schemeId: String) {
        prefs.edit().putString("grid_scheme_id", schemeId).apply()
    }

    fun getGridSchemeId(): String = prefs.getString("grid_scheme_id", "") ?: ""

    fun getAuthToken(): String = prefs.getString("token", "") ?: ""
    fun getUserName(): String = prefs.getString("userName", "操作员") ?: "操作员"
    fun getOrgName(): String = prefs.getString("orgName", "未知组织") ?: "未知组织"
    fun getUserId(): String = prefs.getString("userId", "unknown") ?: "unknown"
    fun getUserCode(): String = prefs.getString("userCode", "unknown") ?: "unknown"
    fun getSignature(): String = prefs.getString("signature", "") ?: ""

    // 添加 clearSession() 方法
    fun clearSession() {
        prefs.edit().clear().apply()
    }

    // 添加检查是否登录的方法
    fun isLoggedIn(): Boolean {
        return prefs.getString("token", null) != null
    }
}